package com.carrot.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.stream.Stream;

/**
 * 业务状态码
 * 组成描述: 错误级别(1位) + 模块标识(2位) + 具体错误码(3位)
 * 0011000
 *
 * 0 为通用正确 一切OK
 * -1 为通用错误
 *
 * 错误级别
 *
 * 1 错误来源于用户
 * 2 错误来源于当前系统
 * 3 示错误来源于第三方服务
 * @author liuhongzhen
 */
@Getter
@AllArgsConstructor
public enum ErrorCodeEnum implements BaseMsg{

    /** 公共模块 */
    CODE_OK(200,"一切OK", ""),
    CODE_ERROR(-1,"默认错误", ""),
    CODE_ERROR_INTERNAL_SERVER_ERROR(1500,"服务器异常", "服务器异常"),
    CODE_ERROR_METHOD_ARGUMENT_NOT_VALID(1501,"方法参数无效", "方法参数无效"),
    CODE_ERROR_CONSTRAINT_VIOLATION(1502,"方法参数校验出错", "方法参数校验出错"),

    CODE_ERROR_LIMITER(100000, "系统繁忙", "当前系统繁忙，请稍后再试"),
    CODE_ERROR_PARAMS_FORMAT(100001,"参数格式不匹配", "参数格式不匹配"),
    CODE_ERROR_WEB_SOCKET(100002,"WebSocket 连接异常", "WebSocket 连接异常"),
    CODE_ERROR_WEB_SOCKET_CLOSED(100003,"WebSocket 连接断开", "WebSocket 连接断开"),
    CODE_ERROR_BLACK_LIST(100004,"没名单限制", "无权访问"),

    UN_LOGIN(100401, "未登录", "抱歉，请先登录"),
    NO_AUTH(100403, "没有权限访问", "没有权限访问"),
    TOKEN_INVALID(100524, "Token无效", "Token无效，请重新登录"),
    TOKEN_EXPIRE(100525, "Token过期", "Token已过期，请重新登录"),
    TOKEN_REFRESH_EXPIRE(100526, "RefreshToken过期", "RefreshToken已过期，请重新登录"),
    TOKEN_REFRESH_FAIL(100527, "刷新Token失败", "刷新Token失败"),
    TOKEN_CHANGE(100528, "Token变更", "检测到您设备IP发生切换，请重新登录"),
    MISS_HEADER(100529, "请求头缺失", "请求头缺失"),
    TOKEN_FAILURE(100530, "生成Token失败", "生成Token失败"),
    CODE_ERROR_AUTH_FOUND_USER(100531, "找不到用户信息", "无该用户信息"),
    TOKEN_DISABLED(100532, "用户封禁", "账户已冻结"),
    ACCOUNT_PASSWORD_ERROR(100533, "密码错误", "密码错误"),


    /** FEIGN */
    CODE_ERROR_FEIGN_NOT_FIND_LOCK(100650, "分布式锁未注入", "当前系统异常，请稍后再试"),
    CODE_ERROR_FEIGN_NOT_FIND_IDEMPOTENT_KEY(100651, "幂等KEY 未指定", "当前系统异常，请稍后再试"),
    CODE_ERROR_FEIGN_NOT_AUTH(100652, "无权限访问", "无权限访问"),


    /** SQL */
    CODE_ERROR_SQL_UPDATE(100700, "更新数据失败", "更新失败，客官请稍后再试"),
    CODE_ERROR_SQL_INSERT(100701,"新增数据失败", "添加失败，客官请稍后再试"),
    CODE_ERROR_SQL_DELETE(100702, "删除数据失败", "删除失败，客官请稍后再试"),
    CODE_ERROR_SQL_INTEGRITY_CONSTRAINT_VIOLATION(100703, "数据主键冲突", "数据主键冲突或者已有该数据！"),
    CODE_ERROR_SQL_NOT_HAVE_DEFAULT_VALUE(100704, "字段没有默认值", "数据异常：{} 字段没有默认值！"),
    CODE_ERROR_SQL_UNKNOWN(100705, "数据异常：未知异常", "数据异常：未知异常，请联系系统管理员 {}"),
    CODE_ERROR_SQL_SELECT(100706, "查询数据失败", "查询失败，客官请稍后再试"),


    CODE_ERROR_SYS_PROCESS(200001,"系统执行出错", "系统执行出错"),
    CODE_ERROR_SYS_OVERTIME(200100,"系统执行超时", "系统执行超时"),
    CODE_ERROR_SYS_LIMITER(200101,"系统限流", "系统限流"),
    CODE_ERROR_SYS_FUNCTION_DEMOTION(200102,"系统功能降级", "系统功能降级"),
    CODE_ERROR_SYS_RESOURCE_EXCEPTION(200103,"系统资源异常", "系统资源异常"),
    CODE_ERROR_SYS_RESOURCE_DEPLETE(200104,"系统资源耗尽", "系统资源耗尽"),
    CODE_ERROR_SYS_LINK_POOL_DEPLETE(200105,"系统连接池耗尽", "系统连接池耗尽"),
    CODE_ERROR_SYS_THREAD_POOL_DEPLETE(200106,"系统线程池耗尽", "系统线程池耗尽"),

    CODE_ERROR_THIRD_PARTY(300000,"调用第三方服务出错", "调用第三方服务出错"),
    CODE_ERROR_INTERFACE_NONENTITY(300100,"接口不存在", "接口不存在"),
    CODE_ERROR_MSG_SERVICE(300110,"消息服务出错", "消息服务出错"),
    CODE_ERROR_MSG_SEND(300111,"消息投递出错", "消息投递出错"),
    CODE_ERROR_MSG_CONSUME(300112,"消息消费出错", "消息消费出错"),
    CODE_ERROR_MSG_SUBSCRIBE(300113,"消息订阅出错", "消息订阅出错"),
    CODE_ERROR_MSG_GROUP_NONENTITY(300114,"消息分组未查到", "消息分组未查到"),
    CODE_ERROR_CACHE_SERVICE(300120,"缓存服务出错", "缓存服务出错"),
    CODE_ERROR_CACHE_KEY_LENGTH(300121,"key 长度超过限制", "key 长度超过限制"),
    CODE_ERROR_CACHE_VALUE_LENGTH(300122,"value 长度超过限制", "value 长度超过限制"),
    CODE_ERROR_CACHE_STORAGE_FULL(300123,"存储容量已满", "存储容量已满"),
    CODE_ERROR_CACHE_NONSUPPORT_DATA(300124,"不支持的数据格式", "不支持的数据格式"),
    CODE_ERROR_CONFIG_SERVICE(300130,"配置服务出错", "配置服务出错"),
    CODE_ERROR_NET_SERVICE(300140,"网络资源服务出错", "网络资源服务出错"),

    CODE_ERROR_THIRD_PARTY_OVERTIME(300200,"第三方系统执行超时", "第三方系统执行超时"),
    CODE_ERROR_MSG_SEND_OVERTIME(300210,"消息投递超时", "消息投递超时"),
    CODE_ERROR_CACHE_SERVICE_OVERTIME(300220,"缓存服务超时", "缓存服务超时"),
    CODE_ERROR_CONFIG_SERVICE_OVERTIME(300230,"配置服务超时", "配置服务超时"),

    CODE_ERROR_DATABASE_SERVICE_OVERTIME(300250,"数据库服务超时", "数据库服务超时"),
    CODE_ERROR_DATABASE_SERVICE(300300,"数据库服务出错", "数据库服务出错"),
    CODE_ERROR_DATABASE_TABLE_NONENTITY(300310,"表不存在", "数据库表不存在: {}"),
    CODE_ERROR_DATABASE_COL_NONENTITY(300311,"列不存在", "数据库列不存在: {}"),
    CODE_ERROR_DATABASE_COL_RELEVANCE_EQ(300312,"多表关联中存在多个相同名称的列", "多表关联中存在多个相同名称的列: {}"),
    CODE_ERROR_DATABASE_PRIMARY_KEY_CONFLICT(300313, "主键冲突", "该数据已存在！"),
    CODE_ERROR_NOTIFY_SERVICE(300400, "通知服务出错", "通知服务出错"),
    CODE_ERROR_NOTIFY_NOTE(300401, "短信提醒服务失败", "短信提醒服务失败"),
    CODE_ERROR_NOTIFY_EMAIL(300402, "邮件提醒服务失败", "邮件提醒服务失败"),

    ;

    private final int code;
    private final String description;
    private final String message;

    public static ErrorCodeEnum getByCode(Integer code){
        return Stream.of(ErrorCodeEnum.values())
                .filter(item -> Integer.valueOf(item.code).equals(code))
                .findFirst().orElse(null);
    }
}

